package com.xl.tools;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class QrCodeUtils {

    public static void main(String[] args) throws IOException, WriterException {
        //读取数据，生成二维码
        String picsStr = "10012303864\n" +
                "10012303863\n" +
                "10012303861\n" +
                "10012303858\n" +
                "10012303856\n" +
                "10012303855\n" +
                "10012303852\n" +
                "10012303851\n" +
                "10012303847\n" +
                "10012303846";
        String[] pics = picsStr.split("\n");
        String baseUrl = "https://wx.chargeland.cn/qcode?m=1&code=";
        File dir = new File("D:/qrcode");
        dir.mkdirs();
        for (String pic : pics) {
            BufferedImage out = drawQrCode(null, baseUrl + pic, "大斑马农创园充电站", pic);
            ImageIO.write(out, "PNG", new File(dir,pic + ".png"));
        }
    }

    public static BufferedImage generateQrCodeByte(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        return deleteWhite(bitMatrix);
    }

    //去白边
    private static BufferedImage deleteWhite(BitMatrix matrix) {
        int QRCOLOR = 0xFF000000; // 默认是黑色
        int BGWHITE = 0xFFFFFFFF; // 背景颜色
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1])) {
                    resMatrix.set(i, j);
                }
            }
        }

        int width = resMatrix.getWidth();
        int height = resMatrix.getHeight();
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, resMatrix.get(x, y) ? QRCOLOR
                        : BGWHITE);
            }
        }
        return image;
    }


    public static BufferedImage drawQrCode(InputStream logo, String qrUrl, String address, String code) throws IOException, WriterException {
        int w = 450;
        int h = 450;
        BufferedImage image = generateQrCodeByte(qrUrl, w, h);
        Font font = new Font("宋体", Font.BOLD, 20);
        Graphics2D graphics = image.createGraphics();
        graphics.setFont(font);
        int lineHeight = 5 * 2 + graphics.getFontMetrics().getHeight();

        //创建一个新的图片，放入文字和内容
        BufferedImage outImage = new BufferedImage(image.getWidth() + 10, image.getHeight() + lineHeight * 2, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D outg = outImage.createGraphics();
        //白色背景
        outg.setPaint(Color.white);
        outg.drawRect(0, 0, outImage.getWidth(), outImage.getHeight());
        outg.fillRect(0, 0, outImage.getWidth(), outImage.getHeight());
        // 画二维码到新的面板
        outg.drawImage(image, (outImage.getWidth() - image.getWidth()) >> 1, 5, image.getWidth(), image.getHeight(), null);
        outg.setFont(font);
        //
        outg.setPaint(Color.black);
        //地址
        int fh = outg.getFontMetrics().getHeight();
        int fy = image.getHeight() + 20 + (fh >> 1);
        int fw = outg.getFontMetrics().stringWidth(address);
        int fx = (outImage.getWidth() - fw) >> 1;
        outg.drawString(address, fx, fy);
        //code
        fy = fy + fh + 6;
        fw = outg.getFontMetrics().stringWidth(code);
        fx = (outImage.getWidth() - fw) >> 1;
        outg.drawString(code, fx, fy);
        //画 logo
        if (logo != null) {
            // 构建绘图对象
            Graphics2D g = image.createGraphics();
            // 读取Logo图片
            BufferedImage logoImg = ImageIO.read(logo);
            // 开始绘制logo图片
            Rectangle rect = new Rectangle((outImage.getWidth() - logoImg.getWidth()) >> -1, (h + 10 - logoImg.getHeight()) >> -1, logoImg.getWidth() + 2, logoImg.getHeight() + 2);
            g.fillRoundRect(rect.x, rect.y, rect.width, rect.height, 2, 2);
            g.drawImage(logoImg, rect.x + 2, rect.y + 2, 32, 32, null);
            g.dispose();
            logoImg.flush();
        }
        outg.dispose();
        return outImage;
    }

}
